//
//  LSMultipleSelectView.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/7/8.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSMultipleSelectView.h"

#define SingleLineHeight 40

@interface LSMultipleSelectView ()
@property (nonatomic, strong) NSArray *selectObjects;
@property (nonatomic, strong) NSMutableArray *selectObjectsM;
@property (nonatomic, strong) NSMutableArray *selectButtonsM;
@property (nonatomic, copy) SelectObjectBlock selectedObjectsBlock;
@property (nonatomic, strong) UIView *backgroundView;
@end

@implementation LSMultipleSelectView

- (instancetype)initWithSelectObjects:(NSArray *)selectObjects andSelectedObjects:(NSArray *)selectedObjects didSelectObjects:(void (^)(NSArray *selectObjects))handler {
    self = [super init];
    if (self) {
        self.selectObjects = selectObjects;
        self.selectObjectsM = selectedObjects.mutableCopy;
        self.selectedObjectsBlock = handler;
        self.backgroundColor = UIColor.whiteColor;
        NSInteger col = selectObjects.count%2;
        NSInteger row = selectObjects.count/2;
        if (col == 1) row += 1;
        CGFloat height = row*SingleLineHeight;
        self.frame = CGRectMake(0, (ScreenHeight-height)/2, ScreenWidth, height);
        for (int i = 0; i < selectObjects.count; i++) {
            NSInteger colInLayout = i%2;
            NSInteger rowInLayout = i/2;
            CGFloat buttonX = ScreenWidth/2*colInLayout;
            CGFloat buttonY = rowInLayout*SingleLineHeight;
            UIButton *objectButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, ScreenWidth/2, SingleLineHeight)];
            [objectButton setTitle:selectObjects[i] forState:UIControlStateNormal];
            [objectButton setTitleColor:hexColor(333333) forState:UIControlStateNormal];
            [objectButton setImage:[UIImage imageNamed:@"yuanquan"] forState:UIControlStateNormal];
            [objectButton setImage:[UIImage imageNamed:@"yuanquan_selected"] forState:UIControlStateSelected];
            [objectButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 8)];
            objectButton.titleLabel.numberOfLines = 2;
            objectButton.titleLabel.font = [UIFont systemFontOfSize:14];
            objectButton.tag = TagFoundation+i;
            [objectButton addTarget:self action:@selector(objectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.selectButtonsM addObject:objectButton];
            [self addSubview:objectButton];
            if (self.selectObjectsM.count > 0) {
                for (int j = 0; j < self.selectObjectsM.count; j++) {
                    if ([selectObjects[i] isEqualToString:self.selectObjectsM[j]]) {
                        objectButton.selected = true;
                    }
                }
            }
        }
    }
    return self;
}

- (void)objectButtonClick:(UIButton *)button {
    NSInteger tag = button.tag-TagFoundation;
    button.selected = !button.isSelected;
    if (button.isSelected) {
        [self.selectObjectsM addObject:self.selectObjects[tag]];
        if ([self.selectObjects[tag] isEqualToString:@"残疾"] || [self.selectObjects[tag] isEqualToString:@"患病"]) {
            // 遍历button数组将“健康”取消选中，先建立保存button的可变数组
            for (UIButton *button in self.selectButtonsM) {
                if ([button.titleLabel.text isEqualToString:@"健康"]) {
                    button.selected = false;
                    [self.selectObjectsM removeObject:button.titleLabel.text];
                }
            }
        } else if ([self.selectObjects[tag] isEqualToString:@"健康"]) {
            for (UIButton *button in self.selectButtonsM) {
                if ([button.titleLabel.text isEqualToString:@"残疾"] || [button.titleLabel.text isEqualToString:@"患病"]) {
                    button.selected = false;
                    [self.selectObjectsM removeObject:button.titleLabel.text];
                }
            }
        } else {
            if ([self.selectObjects[tag] isEqualToString:@"无"]) {
                for (UIButton *button in self.selectButtonsM) {
                    button.selected = false;
                    [self.selectObjectsM removeObject:button.titleLabel.text];
                }
                button.selected = true;
                [self.selectObjectsM addObject:self.selectObjects[tag]];
            } else {
                for (UIButton *button in self.selectButtonsM) {
                    if ([button.titleLabel.text isEqualToString:@"无"]) {
                        button.selected = false;
                        [self.selectObjectsM removeObject:button.titleLabel.text];
                    }
                }
            }
        }
    } else {
        [self.selectObjectsM removeObject:self.selectObjects[tag]];
    }
    if (self.selectedObjectsBlock) {
        self.selectedObjectsBlock(self.selectObjectsM.copy);
    }
}

- (void)setSelectObjectsM:(NSMutableArray *)selectObjectsM {
    _selectObjectsM = selectObjectsM;
}

- (void)setSelectObjects:(NSArray *)selectObjects {
    _selectObjects = selectObjects;
}

- (NSMutableArray *)selectButtonsM {
    if (!_selectButtonsM) {
        _selectButtonsM = [[NSMutableArray alloc] init];
    }
    return _selectButtonsM;
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.backgroundView];
    [window addSubview:self];
    self.alpha = 0.0f;
    WS
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.alpha = 1.0f;
        weakSelf.backgroundView.alpha = 0.4f;
    }];
}

- (void)remove {
    WS
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.alpha = 0.0f;
        weakSelf.backgroundView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [weakSelf.backgroundView removeFromSuperview];
        [weakSelf removeFromSuperview];
    }];
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _backgroundView.backgroundColor = hexColor(000000);
        _backgroundView.alpha = 0.0f;
        _backgroundView.userInteractionEnabled = true;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(remove)];
        [_backgroundView addGestureRecognizer:tap];
    }
    return _backgroundView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
